home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / callin1g / frmdispl.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-08-17  |  17.5 KB  |  412 lines

  1. VERSION 5.00
  2. Begin VB.Form frmDisplay 
  3.    AutoRedraw      =   -1  'True
  4.    BackColor       =   &H00000000&
  5.    BorderStyle     =   3  'Fixed Dialog
  6.    ClientHeight    =   8595
  7.    ClientLeft      =   150
  8.    ClientTop       =   1050
  9.    ClientWidth     =   10365
  10.    ControlBox      =   0   'False
  11.    Icon            =   "frmDisplay.frx":0000
  12.    LinkTopic       =   "Form1"
  13.    MaxButton       =   0   'False
  14.    MinButton       =   0   'False
  15.    MouseIcon       =   "frmDisplay.frx":08CA
  16.    MousePointer    =   99  'Custom
  17.    ScaleHeight     =   8595
  18.    ScaleWidth      =   10365
  19.    ShowInTaskbar   =   0   'False
  20.    WindowState     =   2  'Maximized
  21.    Begin VB.PictureBox picBack 
  22.       AutoRedraw      =   -1  'True
  23.       AutoSize        =   -1  'True
  24.       BackColor       =   &H00000000&
  25.       Height          =   23595
  26.       Left            =   -26400
  27.       ScaleHeight     =   23535
  28.       ScaleWidth      =   27420
  29.       TabIndex        =   2
  30.       Top             =   -22680
  31.       Visible         =   0   'False
  32.       Width           =   27480
  33.    End
  34.    Begin VB.PictureBox picBuf 
  35.       AutoRedraw      =   -1  'True
  36.       AutoSize        =   -1  'True
  37.       BackColor       =   &H00000000&
  38.       BorderStyle     =   0  'None
  39.       Height          =   6735
  40.       Left            =   -7440
  41.       ScaleHeight     =   6735
  42.       ScaleWidth      =   8850
  43.       TabIndex        =   1
  44.       Top             =   -5520
  45.       Width           =   8850
  46.    End
  47.    Begin VB.Timer tmrMove 
  48.       Enabled         =   0   'False
  49.       Interval        =   50
  50.       Left            =   1680
  51.       Top             =   7440
  52.    End
  53.    Begin VB.PictureBox picSpr 
  54.       AutoRedraw      =   -1  'True
  55.       AutoSize        =   -1  'True
  56.       Height          =   6195
  57.       Left            =   -4440
  58.       Picture         =   "frmDisplay.frx":0A1C
  59.       ScaleHeight     =   6135
  60.       ScaleWidth      =   6135
  61.       TabIndex        =   0
  62.       Top             =   -4680
  63.       Visible         =   0   'False
  64.       Width           =   6195
  65.    End
  66. Attribute VB_Name = "frmDisplay"
  67. Attribute VB_GlobalNameSpace = False
  68. Attribute VB_Creatable = False
  69. Attribute VB_PredeclaredId = True
  70. Attribute VB_Exposed = False
  71. Option Explicit
  72. '========================================================='
  73. '========================================================='
  74. '=============== RPG Game Version 0.0.2 =================='
  75. '============== Written by Matthew Eagar ================='
  76. '============ Compiled in Visual Basic 6.0 ==============='
  77. '========================================================='
  78. '========================================================='
  79. '   This program is an example of an RPG game engine made
  80. '   with VB 6.0.  I drew all the graphics in MS Paint,
  81. '   and all coding is origional.
  82. '   This isn't ment to be a full game, just a working engine.
  83. '   there is no actual objective.  I havn't yet got doors
  84. '   working, because that would require me to draw some more
  85. '   textures for the insides of houses, which takes FOREVER!
  86. '   Also, the textures could REALLY use some work,
  87. '   as they were drawn in MS Paint.
  88. '   This program may not run well on some computers.
  89. '   The method used, bitblt, works well, but isn't designed for games.
  90. '   It runs fine on a Pentium 233, but slow on a P75.  I havn't tested
  91. '   it on anything in between those.
  92. '   I'm still working on this, so look for me to post newer versions
  93. '   of it.  It'll remain free, and it's really ment for educational purposes.
  94. '   Please contact me with ANY questions, comments, suggestions, or problems,
  95. '   ANY input is welcome:
  96. '   email:  meagar@home.com
  97. '   ICQ:    45058462
  98. '   Also, I havn't tested this on any computer running anything less then VB6.
  99. '   I did run it in vb5, but it took some work.
  100. '   You will need the VB6 runtime files the use this.
  101. '   Updates to Version 2:
  102. '   Added side scrolling and top scrolling
  103. '   Rechanged the map size from 13x11 to 30x30 tiles to accomidate side scrolling
  104. '   Added Bridge Tiles for bridge construction
  105. '   Added sound effects
  106. '   re-wrote most of movement code
  107. Dim animX As Integer    'holds the current x location of the animation frame
  108. Dim animY As Integer    'holds the current y location of the animation frame
  109. Dim direction As Integer    'the direction the characters facing
  110. Dim charX As Integer       'holds the character's x coords
  111. Dim charY As Integer       'holds the character's y coords
  112. Dim lastX As Integer    'holds the character's last y coords
  113. Dim lastY As Integer    'holds the character's last x coords
  114. Dim BackBuilt As Integer 'determines if the back ground needs to be built
  115. Dim Speed As Integer    'holds the current speed, set by pressing the + or - keys
  116. Dim mapx As Integer     'holds the current map x number
  117. Dim mapy As Integer     'holds the current map y number
  118. Dim MapName As String   'holds the name of the map
  119. Dim screenX As Integer  'holds the current location of the screen on the map
  120. Dim screenY As Integer  'holds the current location of the screen on the map
  121. Dim charPosX As Integer 'holds the coords to center the character on the screen
  122. Dim charPosY As Integer 'holds the coords to center the character on the screen
  123. Dim sound As Boolean     'holds whether to play sounds or not
  124. 'symbolic constants
  125. 'directions
  126. Const dLEFT As Integer = 1    'left direction
  127. Const dUP As Integer = 2      'up direction
  128. Const dRIGHT As Integer = 3   'right direction
  129. Const dDOWN  As Integer = 4   'down direction
  130. 'animation frames
  131. Const aLEFT As Integer = 2    'left animation
  132. Const aUP As Integer = 104    'up animation
  133. Const aRIGHT As Integer = 206 'right animation
  134. Const aDOWN As Integer = 308  'down animation
  135. 'when the user presses a key
  136. Private Sub picBuf_KeyDown(KeyCode As Integer, Shift As Integer)
  137. Dim X As Integer 'counting variable
  138. 'if movement, turn the mouse cursor into the invisible icon.
  139. 'simply making a mouse cursor that was invisible is easier
  140. 'then using API calls.
  141. frmDisplay.MouseIcon = frmTextures.picInvisible.Picture
  142. 'copy the current location of the character into the lastx and lasty variables.
  143. lastX = screenX
  144. lastY = screenY
  145. 'determine how to act, based on which key the user presses.
  146. Select Case KeyCode
  147. Case Is = 37    'left arrow key
  148.     animX = aLEFT   'set the animation frame to the proper direction
  149.     direction = dLEFT 'set the direction
  150. Case Is = 38    'up arrow key
  151.     animX = aUP 'set the animation frame to the proper direction
  152.     direction = dUP
  153. Case Is = 39    'right arrow key
  154.     animX = aRIGHT
  155.     direction = dRIGHT
  156. Case Is = 40    'down arrow key
  157.     animX = aDOWN
  158.     direction = dDOWN
  159. Case Is = 27    'escape key
  160.     'ask if the user would like to exit
  161.     If MsgBox("Are you sure you would like to exit?", vbYesNo + vbDefaultButton2 + vbQuestion, "Exit?") = vbYes Then End
  162. Case Is = 109   'minus key - increases screen size
  163.     Speed = Speed - 2
  164.     If Speed < 5 Then Speed = 5
  165. Case Is = 107   'plus key - decreases screen size
  166.     Speed = Speed + 2
  167.     If Speed > 20 Then Speed = 20
  168. Case Is = 83    'the S key
  169.     'turn sound on or off
  170.     If sound = True Then
  171.         sound = False
  172.     Else
  173.         sound = True
  174.     End If
  175. End Select
  176. 'see if the movement timer should be enabled
  177. If KeyCode >= 37 And KeyCode <= 40 Then 'if a direction key's been pressed
  178.     tmrMove.Enabled = True
  179. End If
  180. End Sub
  181. Private Sub picBuf_KeyUp(KeyCode As Integer, Shift As Integer)
  182. 'disable the movement timer
  183. tmrMove.Enabled = False
  184. End Sub
  185. Private Sub Form_Load()
  186. 'initialize the variables
  187. animX = 2
  188. animY = 1
  189. screenX = 10
  190. screenY = 10
  191. charX = screenX + charPosX + 25
  192. charY = screenY + charPosY + 25
  193. sound = True
  194. BackBuilt = False
  195. 'maps are loaded in the following way:
  196. 'take the mapX, then add the letter 'a' then take the mapY, then add ".map"
  197. 'so, the first map is called 0a0.map, the map beside it is called
  198. '1a0.map, and the map above the first is called 0a1.map
  199. 'eventually the middle letter will stand for the area, eg a = lev 1, b = lev 2
  200. mapx = 0    'the current map
  201. mapy = 0
  202. Speed = 15  'set the initial walking speed
  203. 'set the size of the main picture box
  204. 'change the number to make the picture bigger or smaller, but the number can't be
  205. 'large then 1 or smaller then 0
  206. picBuf.Height = Int(Screen.Height * 0.8)
  207. picBuf.Width = Int(Screen.Width * 0.8)
  208. picBuf.Left = (Screen.Width - picBuf.Width) / 2 'center the main picture box
  209. picBuf.Top = (Screen.Height - picBuf.Height) / 2
  210. 'charPosX is the distance of the character from the left side of the screen
  211. 'charPosY is the distance from the top
  212. charPosX = picBuf.Width * 0.03  'center the character on the screen
  213. charPosY = picBuf.Height * 0.03
  214. Call BuildBack  'build the back ground
  215. Call redrawPic  'load the pic into the main pic box
  216. End Sub
  217. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, animX As Single, animY As Single)
  218. 'turn the mouse icon into the visible icon
  219. frmDisplay.MouseIcon = frmTextures.picVisible.Picture
  220. End Sub
  221. Private Sub tmrMove_Timer()
  222. Dim X As Integer
  223. If touching() <> 1 Then
  224.     'move the character in the proper direction
  225.     If direction = dLEFT Then
  226.         screenX = screenX - Speed
  227.     ElseIf direction = dUP Then
  228.         screenY = screenY - Speed
  229.     ElseIf direction = dRIGHT Then
  230.         screenX = screenX + Speed
  231.     ElseIf direction = dDOWN Then
  232.         screenY = screenY + Speed
  233.     End If
  234.         
  235.     charX = screenX + charPosX + 25
  236.     charY = screenY + charPosY + 25
  237.     Call redrawPic 'redraws the form
  238.     animY = animY + 51 'advance the frame, each frame is 50 pixels wide, + a 1 pixel border
  239.     'there are 8 frames in the character's animation: this sees if the last frame has
  240.     'been shown. if it has, it resets it to the first.
  241.     If animY >= 408 Then
  242.         animY = 1  'goes to first frame
  243.         If sound = True Then Call sndPlaySound(App.Path & "\" & "1.wav", SND_ASYNC) 'play the foot step sound
  244.     ElseIf animY >= 204 And animY <= 255 Then
  245.         If sound = True Then Call sndPlaySound(App.Path & "\" & "1.wav", SND_ASYNC)  'play the foot step sound
  246.     End If
  247. End If
  248. 'see if the back ground has been built
  249. If BackBuilt = False Then
  250.     'build the background
  251.     Call BuildBack
  252.     BackBuilt = True
  253. End If
  254. 'see if the character has left the screen, by checking if the character's
  255. 'x or y position is greater then the total amount of tiles
  256. 'so far this only works properly in 1024 x 768 resolution
  257. If screenX + 25 >= 1200 - charPosX Then 'if the character has left the right side of the screen
  258.     mapx = mapx + 1 'set the current map name to the next map name
  259.     screenX = 10 - charPosX - 25 'set the character's position back to the left side of the screen
  260.     Call BuildBack  'redraw the back ground
  261. ElseIf screenX + 25 <= 0 - charPosX Then 'see if the character has left the left side of the screen
  262.     mapx = mapx - 1 'set the current map name to the next map name
  263.     screenX = 1190 - charPosX - 25 'set the character position to the right side of the screen
  264.     Call BuildBack  'redraw the back ground
  265. ElseIf screenY + 25 <= 0 - charPosY Then  'see if the character has left the top of the screen
  266.     mapy = mapy + 1 'set the current map name to the next map name
  267.     screenY = 1190 - charPosY - 25 'set the characters position to the bottom of the screen
  268.     Call BuildBack  'redraw the back ground
  269. ElseIf screenY + 25 >= 1200 - charPosY Then 'see if the character has left the bottom of the screen
  270.     mapy = mapy - 1 'set the current map name to the next map name
  271.     screenY = 10 - charPosY - 25  'move the character to the top of the screen
  272.     Call BuildBack  'redraw the back ground
  273. End If
  274. End Sub
  275. 'assembles the back ground
  276. Sub BuildBack()
  277. 'this sub builds the back ground.  It is called only once per map,
  278. 'as the map is built in a hidden pic box, and kept untill the next map is needed.
  279. Dim g As Integer    'counting variable
  280. Dim a As Integer    'temp variable
  281. Dim X As Integer    'holds x coords of tile
  282. Dim Y As Integer    'holds y coords of tile
  283. On Error GoTo errHandler
  284. 'set the name of the map
  285. If Right(App.Path, 1) = "\" Then
  286.     MapName = App.Path & mapx & "a" & mapy & ".map"
  287.     MapName = App.Path & "\" & mapx & "a" & mapy & ".map"
  288. End If
  289. 'read the textures and the walkable values from the map file
  290. Open MapName For Input As #1
  291.     For g = 0 To 899
  292.         Input #1, Texture(g), Walkable(g)
  293.     Next g
  294. Close
  295. 'clear the picture box which will hold the back ground
  296. picBack.Cls
  297. X = 0
  298. Y = 0
  299. 'loop through each tile, getting it with bitblt from frmTextures, and putting it into
  300. 'the picBack pic box.
  301. For g = 0 To 899
  302.     tileLeft(g) = X
  303.     tileTop(g) = Y
  304.     a = BitBlt(picBack.hDC, X, Y, 40, 40, frmTextures.picTextures(Texture(g)).hDC, 0, 0, SRCCOPY)
  305.     Y = Y + 40
  306.     'if a column has been finished, goto the next one
  307.     If Y >= 1200 Then
  308.         Y = 0
  309.         X = X + 40
  310.     End If
  311. Next g
  312. 'by-pass error handler
  313. GoTo endsub
  314. 'for errors
  315. errHandler:
  316. MsgBox "Error number " & Err.Number & ": " & Err.Description, vbOKOnly + vbCritical, "Dragon Lore"
  317. MsgBox MapName & " was not found or was corrupted.  Please re-install this program."
  318. endsub:
  319. End Sub
  320. Sub redrawPic()
  321. 'this function draws the picture to the screen.
  322. 'black out the old picture
  323. picBuf.Cls
  324. 'Copy the back ground to the buffer pic box
  325. Call BitBlt(picBuf.hDC, 0, 0, 2900, 9500, picBack.hDC, screenX, screenY, SRCCOPY)
  326. 'Copy the first layer of the sprite to the buffer
  327. Call BitBlt(picBuf.hDC, charPosX, charPosY, 50, 50, picSpr.hDC, animX + 50, animY, SRCAND)
  328. 'Copy the second layer of the sprite to the buffer, for transparent effect.
  329. Call BitBlt(picBuf.hDC, charPosX, charPosY, 50, 50, picSpr.hDC, animX, animY, SRCINVERT)
  330. 'refresh the picture
  331. picBuf.Refresh
  332. End Sub
  333. Private Function touching() As Integer
  334. Dim g As Integer ' counting variable
  335. Dim tmpX As Integer
  336. Dim tmpY As Integer
  337. 'this looks at the direction the character is moving, and sees if the next step
  338. 'will put the character onto a tile which has a walkable value of 1, which is
  339. 'either water trees or a building.  If it is, it returns 1. if not, it returns 0.
  340. tmpX = 0
  341. tmpY = 0
  342. 'check each tile
  343. 'I'm looking for ways to OPTIMIZE this!! Email me with suggestions!
  344. For g = 0 To 899
  345.     'only proceed to check a tile if it is within a certain radius of the character,
  346.     'and if it is a tree/water/wall
  347.     If Abs((charX + 25) - (tileLeft(g) + 20)) < 250 And Abs((charY + 25) - (tileTop(g) + 20)) < 250 And Walkable(g) = 1 Then
  348.         If direction = dLEFT Then   'if the character is walking left
  349.             'check the left side of the character
  350.             If charX - 25 - Speed > tileLeft(g) And charX - 25 - Speed < tileLeft(g) + 40 Then
  351.                 'check the lower left corner
  352.                 If charY + 25 > tileTop(g) And charY + 25 < tileTop(g) + 40 Then
  353.                     GoTo endsub
  354.                 'check the top left corner
  355.                 ElseIf charY - 25 > tileTop(g) And charY - 25 < tileTop(g) + 40 Then
  356.                     GoTo endsub
  357.                 'check the center of the left side
  358.                 ElseIf charY > tileTop(g) And charY < tileTop(g) + 40 Then
  359.                     GoTo endsub
  360.                 End If
  361.             End If
  362.         ElseIf direction = dUP Then 'if the character is walking up
  363.             'check the top side of the character
  364.             If charY - 25 - Speed > tileTop(g) And charY - 25 - Speed < tileTop(g) + 40 Then
  365.                 'check the top right corner
  366.                 If charX + 25 > tileLeft(g) And charX + 25 < tileLeft(g) + 40 Then
  367.                     GoTo endsub
  368.                 'check to top left corner
  369.                 ElseIf charX - 25 > tileLeft(g) And charX - 25 < tileLeft(g) + 40 Then
  370.                     GoTo endsub
  371.                 'check the center of the top side
  372.                 ElseIf charX > tileLeft(g) And charX < tileLeft(g) + 40 Then
  373.                     GoTo endsub
  374.                 End If
  375.             End If
  376.         ElseIf direction = dRIGHT Then  'if the character is walking right
  377.             'check the right side of the character
  378.             If charX + 25 + Speed > tileLeft(g) And charX + 25 + Speed < tileLeft(g) + 40 Then
  379.                 'check the right top corner
  380.                 If charY - 25 > tileTop(g) And charY - 25 < tileTop(g) + 40 Then
  381.                     GoTo endsub
  382.                 ElseIf charY + 25 > tileTop(g) And charY + 25 < tileTop(g) + 40 Then
  383.                     GoTo endsub
  384.                 'check the center of the right side
  385.                 ElseIf charY > tileTop(g) And charY < tileTop(g) + 40 Then
  386.                     GoTo endsub
  387.                 End If
  388.             End If
  389.         ElseIf direction = dDOWN Then   'if the character is walking down
  390.             'check the bottom side of the character
  391.             If charY + 25 > tileTop(g) And charY + 25 < tileTop(g) + 40 Then
  392.                 'check the bottom right corner
  393.                 If charX + 25 > tileLeft(g) And charX + 25 < tileLeft(g) + 40 Then
  394.                     GoTo endsub
  395.                 ElseIf charX - 25 > tileLeft(g) And charX - 25 < tileLeft(g) + 40 Then
  396.                     GoTo endsub
  397.                 'check the middle of the bottom side
  398.                 ElseIf charX > tileLeft(g) And charX < tileLeft(g) + 40 Then
  399.                     GoTo endsub
  400.                 End If
  401.             End If
  402.         End If
  403.     End If
  404. Next g
  405. touching = 0
  406. GoTo endFunct
  407. endsub:
  408. 'reset the character location
  409. touching = 1
  410. endFunct:
  411. End Function
  412.